<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
	/* 首页 */
    public function index()
    {
    	$User = M("admin_example");
      $a=$User->where('e_is=1')->select();
      
      $banner = M("banner");
      $b=$banner->select();
      $this->assign("banner", $b);
      $this->assign("hot_goods", $a);
    	$this->display();
    }
    /* 首页 */

    /* 分类 */
    public function category()
    {
        $User = M("category");
    	$category=$User->select();
    	$this->assign("category", $category);
    	$this->display();
    }
    /* 分类 */
  
       
      /*
      * 用户注册 
      */
      public function signup(){
        $data = I('post.');
        $User = M("User");
        $datanum = $User->where(['username'=>$data[username]])->find();
        if ($datanum){
            $msg=['msg'=>'注册失败','code'=>'200'];
            $this->ajaxReturn($msg);
        }else{
            $data['password'] = md5($data[password]);   //md5加密密码
            $User->add($data);                          
            $msg=['msg'=>'注册成功','code'=>'400'];
            $this->ajaxReturn($msg);
        }
        
      }
      
      /* 用户登陆 */
      public function login(){
        $data = I('post.');
        $User = M("User");
        $data['password'] = md5($data[password]);
        $Verification['username'] = $data[username];
        $Verification['password'] = $data[password];
        $datanum = $User->where($Verification)->find();
        if ($datanum){
            session_start();
            $_SESSION['username']=$Verification['username'];
            $_SESSION['password']=$Verification['password'];
            $_SESSION['id']=$datanum['id'];
            $msg=['code'=>'200'];
            $this->ajaxReturn($msg);
        }else{
            $msg=['msg'=>'用户名或密码不匹配','code'=>'400'];
            $this->ajaxReturn($msg);
        }
      }
      /* 用户登陆 */
      
      /* 用户页面 */
      public function user(){
        if($_SESSION){
          $User = M("user");
          $user_name = $_SESSION['username'];
          $user_info=$User->where(['username' => $user_name])->find();
          $this->assign("user_info", $user_info);
          $this->display();
        }else{
          header("Location: /home/index/user_login");
        }
        
      }
      /* 用户页面 */

      /* 退出登陆 */
      public function logout(){
        session(null);
        if($_SESSION){
          $msg=['code'=>'400','msg'=>'退出失败'];
          $this->ajaxReturn($msg);
        }else{
          $msg=['code'=>'200','msg'=>'退出成功'];
          $this->ajaxReturn($msg);
        }
      }
      /* 退出登陆 */
      
      /* 商品页面 */
      public function category_content(){
        $id = $_GET['id'];
        $User = M("admin_example");
        $category_name = M("category");
        $goods=$User->where(['e_select' => $id])->select();
        $category_title = $category_name->where(['id' => $id])->find();
        $this->assign("goods", $goods);
        $this->assign("category_title", $category_title);
        $this->display();
      }
      /* 商品页面 */

      /* 搜索 */
      public function search(){
        $id = $_GET['keyword'];
        if(!empty($id)){  
          $where['e_varchar'] = array('like','%'.$id.'%');
          $test = M('admin_example')->where($where)->select();
        }  
        $this->assign("goods", $test);
        $this->display();
      }
      /* 搜索 */

      /* 提交订单 */
      public function order_content_list(){
        if($_SESSION){
          $no = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
          $noandtime = $time=intval(time()).$no;
          //检测是否存在
         
          $db = M('order');
          $info = $db->where(['user_number'=>$noandtime])->find();
          $order_number = $noandtime;
          
          $data = [
            'order_type'     => I('post.order_type', '', 'trim'),
            'making_type'    => I('post.making_type', '', 'trim'),
            'video_type'     => I('post.video_type', '', 'trim'),
            'pv_type'        => I('post.pv_type', '', 'trim'),
            'order_number'   => $order_number,
            'remarks'        => I('post.remarks','','trim'),
            'user_order'     => $_SESSION['id'],
          ];
          
          $db->add($data);
          $this->success('已提交订单', U('/home/index/order_content'));
        }else{
          header("Location: /home/index/user_login");
        }
      }
      /* 提交订单 */

      /* 查询订单 */
      public function order_content(){
        $User = M("order");
        $order_list = $User->where(['user_order'=>$_SESSION['id']])->select();
        $this->assign("order_list", $order_list);
        $this->display();
      }
      /* 查询订单 */
     
       /* 商品页面 */
       public function goods(){
          $id = $_GET['id'];
          $User = M("admin_example");
          $good = $User->where(['id'=>$id])->find();
          $this->assign("good", $good);
          $this->display();
      }
      /* 商品页面 */

       /* 订单页面 */
       public function order_add(){
        $id = $_GET['id'];
        $User = M("admin_example");
        $good = $User->where(['id'=>$id])->find();
        $this->assign("order", $good);
        $this->display();
      }
      /* 订单页面 */
}
